package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.DateUtils;
import com.settlement.system.common.util.NumberUtils;
import com.settlement.system.mapper.SysClientRegisterMapper;
import com.settlement.system.model.entity.SysClientRegister;
import com.settlement.system.model.form.SysClientRegisterForm;
import com.settlement.system.model.query.SysClientRegisterQuery;
import com.settlement.system.model.vo.SysClientRegisterImportVo;
import com.settlement.system.service.SysClientRegisterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 客服登记表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-03-29
 */
@Service
public class SysClientRegisterServiceImpl extends ServiceImpl<SysClientRegisterMapper, SysClientRegister> implements SysClientRegisterService {

    @Override
    public Page<SysClientRegister> getPage(SysClientRegisterQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysClientRegister> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysClientRegister(SysClientRegisterForm sysClientRegisterForm) {
        SysClientRegister sysClientRegister = new SysClientRegister();
        BeanUtil.copyProperties(sysClientRegisterForm, sysClientRegister);
        sysClientRegister.setCreateTime(null);
        return this.saveOrUpdate(sysClientRegister);
    }

    @Override
    public SysClientRegisterForm getSysClientRegisterFormData(Long id) {
        SysClientRegister sysClientRegister = this.getById(id);
        SysClientRegisterForm sysClientRegisterForm = new SysClientRegisterForm();
        BeanUtil.copyProperties(sysClientRegister, sysClientRegisterForm);
        return sysClientRegisterForm;
    }

    @Override
    public boolean deleteSysClientRegister(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysClientRegisterImportVo> voList) {
        voList.removeIf(item -> Objects.equals("日期", item.getRegisterDate()));
        List<SysClientRegister> sysClientRegisterList = new ArrayList<>();
        for (SysClientRegisterImportVo importVo : voList) {
            SysClientRegister sysClientRegister = new SysClientRegister();
            BeanUtil.copyProperties(importVo, sysClientRegister, "registerDate", "payAmount", "receiveAmount");
            if (DateUtils.convertStringToLocalDateTime(importVo.getRegisterDate()) != null) {
                sysClientRegister.setRegisterDate(DateUtils.convertStringToLocalDateTime(importVo.getRegisterDate()).toLocalDate());
            }
            // 支付金额财务
            sysClientRegister.setPayAmount(NumberUtils.convertBigDecimal(importVo.getPayAmount()));
            // 收入金额财务
            sysClientRegister.setReceiveAmount(NumberUtils.convertBigDecimal(importVo.getReceiveAmount()));
            sysClientRegisterList.add(sysClientRegister);
        }
        if (sysClientRegisterList.size() > 0) {
            this.saveOrUpdateBatch(sysClientRegisterList);
        }
    }

    @Override
    public List<SysClientRegisterImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysClientRegister> sysClientRegisterList = this.getBaseMapper().selectBatchIds(ids);
        List<SysClientRegisterImportVo> voList = new ArrayList<>();
        for (SysClientRegister sysClientRegister : sysClientRegisterList) {
            SysClientRegisterImportVo vo = new SysClientRegisterImportVo();
            BeanUtil.copyProperties(sysClientRegister, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<SysClientRegister> getByOrderNo(String orderNo) {
        return this.list(new LambdaQueryWrapper<SysClientRegister>().eq(SysClientRegister::getOrderNo, orderNo));
    }
}
